package com.top10.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.top10.bean.JwtUtil;
import com.top10.bean.ResponseBean;
import com.top10.entity.Goods;
import com.top10.entity.Shop;
import com.top10.entity.UserCollect;
import com.top10.service.IGoodsService;
import com.top10.service.IShopService;
import com.top10.service.IUserCollectService;
import com.top10.utils.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * 用户收藏Controller
 *
 * @author ellen
 * @version 1.0
 * @date 2018/11/12
 */
@RestController
@RequestMapping("/userCollect")
public class UserCollectController {

    @Autowired
    IShopService shopService;
    @Autowired
    IGoodsService goodsService;
    @Autowired
    IUserCollectService userCollectService;

    /**
     * 店铺收藏
     *
     * @param id
     * @return
     */
    @RequestMapping("/manager/shopCollect")
    public ResponseBean shopCollect(String id, HttpServletRequest request) {
        if (id == null) {
            return ResponseBean.createError("店铺id不能为空");
        }
        Shop shop = shopService.selectOne(new EntityWrapper<Shop>().eq("uuid", id));
        if (shop == null) {
            return ResponseBean.createError("找不到该店铺");
        }

        String uuid = JwtUtil.getUuidByToken(request);
        UserCollect uc = userCollectService.selectOne(new EntityWrapper<UserCollect>().eq("user_uuid", uuid).eq("target_uuid", shop.getUuid()));
        if (uc != null) {
            if (uc.getIsdeleted() == false) {
                return ResponseBean.createError("已经收藏过该店铺");
            }
            uc.setIsdeleted(false);
            uc.setUpdatetime(new Date());
            userCollectService.updateById(uc);

            return ResponseBean.createSuccess().addData("userCollect", uc);
        }

        //填充收藏数据
        UserCollect userCollect = new UserCollect();
        userCollect.setUuid(RandomUtils.makeUUID());
        userCollect.setUserUuid(uuid);
        userCollect.setType(1);
        userCollect.setTargetUuid(shop.getUuid());
        userCollect.setIsdeleted(false);
        userCollect.setCreatetime(new Date());
        userCollect.setUpdatetime(new Date());
        userCollect.setCreator(uuid);

        userCollectService.insert(userCollect);

        return ResponseBean.createSuccess().addData("userCollect", userCollect);
    }

    /**
     * 取消店铺收藏
     *
     * @param id
     * @param request
     * @return
     */
    @RequestMapping("/manager/cancelShopCollect")
    public ResponseBean cancelShopCollect(String id, HttpServletRequest request) {
        if (id == null) {
            return ResponseBean.createError("店铺id不能为空");
        }
        Shop shop = shopService.selectOne(new EntityWrapper<Shop>().eq("uuid", id));
        if (shop == null) {
            return ResponseBean.createError("找不到该店铺");
        }

        String uuid = JwtUtil.getUuidByToken(request);
//        userCollectService.delete(new EntityWrapper<UserCollect>().eq("user_uuid", uuid).eq("target_uuid", shop.getUuid()));
        UserCollect userCollect = userCollectService.selectOne(new EntityWrapper<UserCollect>().eq("user_uuid", uuid).eq("target_uuid", shop.getUuid()));
        if (userCollect == null) {
            return ResponseBean.createError("找不到该收藏");
        }

        userCollect.setIsdeleted(true);
        userCollect.setUpdatetime(new Date());
        userCollectService.updateById(userCollect);

        return ResponseBean.createSuccess().setMessage("已取消收藏");
    }

    /**
     * 店铺收藏状态
     *
     * @param shopId
     * @param request
     * @return
     */
    @RequestMapping("/manager/shopCollectStatus")
    public ResponseBean shopCollectStatus(String shopId, HttpServletRequest request) {
        if (shopId == null) {
            return ResponseBean.createError("店铺id不能为空");
        }
        Shop shop = shopService.selectOne(new EntityWrapper<Shop>().eq("uuid", shopId));
        if (shop == null) {
            return ResponseBean.createError("找不到该店铺");
        }

        String uuid = JwtUtil.getUuidByToken(request);
        Integer status = 0; //收藏状态 0未收藏 1已收藏
        UserCollect uc = userCollectService.selectOne(new EntityWrapper<UserCollect>().eq("user_uuid", uuid).eq("target_uuid", shop.getUuid()));
        if (uc != null && uc.getIsdeleted() == false) {
            status = 1;
        }

        return ResponseBean.createSuccess().addData("status", status);
    }

    /**
     * 收藏商品
     *
     * @param goodsId
     * @param request
     * @return
     */
    @RequestMapping("/manager/goodsCollect")
    public ResponseBean goodsCollect(String goodsId, HttpServletRequest request) {
        if (goodsId == null) {
            return ResponseBean.createError("商品id不能为空");
        }
        Goods goods = goodsService.selectOne(new EntityWrapper<Goods>().eq("uuid", goodsId));
        if (goods == null) {
            return ResponseBean.createError("找不到该商品");
        }

        String uuid = JwtUtil.getUuidByToken(request);
        UserCollect uc = userCollectService.selectOne(new EntityWrapper<UserCollect>().eq("user_uuid", uuid).eq("target_uuid", goods.getGoodsuuid()));
        if (uc != null) {
            if (uc.getIsdeleted() == false) {
                return ResponseBean.createError("已经收藏过该商品");
            }
            uc.setIsdeleted(false);
            uc.setUpdatetime(new Date());
            userCollectService.updateById(uc);

            return ResponseBean.createSuccess().addData("userCollect", uc);
        }

        //填充收藏数据
        UserCollect userCollect = new UserCollect();
        userCollect.setUuid(RandomUtils.makeUUID());
        userCollect.setUserUuid(uuid);
        userCollect.setType(1);
        userCollect.setTargetUuid(goods.getGoodsuuid());
        userCollect.setIsdeleted(false);
        userCollect.setCreatetime(new Date());
        userCollect.setUpdatetime(new Date());
        userCollect.setCreator(uuid);

        userCollectService.insert(userCollect);

        return ResponseBean.createSuccess().addData("userCollect", userCollect);
    }

    /**
     * 取消商品收藏
     *
     * @param goodsId
     * @param request
     * @return
     */
    @RequestMapping("/manager/cancelGoodsCollect")
    public ResponseBean cancelGoodsCollect(String goodsId, HttpServletRequest request) {
        if (goodsId == null) {
            return ResponseBean.createError("商品id不能为空");
        }
        Goods goods = goodsService.selectOne(new EntityWrapper<Goods>().eq("uuid", goodsId));
        if (goods == null) {
            return ResponseBean.createError("找不到该商品");
        }

        String uuid = JwtUtil.getUuidByToken(request);
        UserCollect userCollect = userCollectService.selectOne(new EntityWrapper<UserCollect>().eq("user_uuid", uuid).eq("target_uuid", goods.getGoodsuuid()));
        if (userCollect == null) {
            return ResponseBean.createError("找不到该收藏");
        }

        userCollect.setIsdeleted(true);
        userCollect.setUpdatetime(new Date());
        userCollectService.updateById(userCollect);

        return ResponseBean.createSuccess().setMessage("已取消收藏");
    }

    /**
     * 商品收藏状态
     *
     * @param goodsId
     * @param request
     * @return
     */
    @RequestMapping("/manager/goodsCollectStatus")
    public ResponseBean goodsCollectStatus(String goodsId, HttpServletRequest request) {
        if (goodsId == null) {
            return ResponseBean.createError("商品id不能为空");
        }
        Goods goods = goodsService.selectOne(new EntityWrapper<Goods>().eq("uuid", goodsId));
        if (goods == null) {
            return ResponseBean.createError("找不到该商品");
        }

        String uuid = JwtUtil.getUuidByToken(request);
        Integer status = 0;
        UserCollect uc = userCollectService.selectOne(new EntityWrapper<UserCollect>().eq("user_uuid", uuid).eq("target_uuid", goods.getGoodsuuid()));
        if (uc != null && uc.getIsdeleted() == false) {
            status = 1;
        }

        return ResponseBean.createSuccess().addData("status", status);
    }

}